import * as fs from "fs";
import * as xlsx from "node-xlsx";
/**
 * 工具类
 */
export class Utils {
    /**
     * 读取文件内容
     * @param filePath 文件名称路径
     * @returns 
     */
    static readFile(filePath: string) {
        return new Promise<Buffer>((resolve, reject) => {
            fs.readFile(filePath, (err, data: Buffer) => {
                if (err) {
                    reject(err)
                }
                resolve(data);
            })
        })
    }
    /**
     * 写入文件
     * @param filePath  文件路径
     * @param data 要写入文件的数据
     * @returns 
     */
    static writeFile(filePath: string, data: string) {
        return new Promise((resolve, reject) => {
            fs.writeFile(filePath, data, (err) => {
                if (err) {
                    reject(err)
                }
                resolve(null);
            })
        })
    }
    /**
     * 读取目录
     * @param dirPath 目录路径
     * @returns 
     */
    static readdir(dirPath: string) {
        return new Promise<string[]>((resolve, reject) => {
            fs.readdir(dirPath, (err, files: string[]) => {
                if (err) {
                    reject(err)
                }
                resolve(files);
            })
        })
    }

    /**
    * 删除文件
    * @param dirPath 删除文件
    * @returns 
    */
    static unlink(filePath: string) {
        return new Promise((resolve, reject) => {
            fs.unlink(filePath, (err,) => {
                if (err) {
                    reject(err)
                }
                resolve(null);
            })
        })
    }


    /**
     * 删除目录下所有文件
     * @param dirPath 目录路径
     * @returns 
     */
    static async unlinkDir(dirPath: string) {
        let paths = await this.readdir(dirPath);
        paths.forEach(async (path) => {
            await this.unlink(`${dirPath}${path}`);
            console.log(`delete ${path} success!`);
        })
    }
    /**
     * 是否为中文
     * @param str 
     * @returns 
     */
    static isChinese(str: string): boolean {
        let reg = /[\u4e00-\u9fa5]/g;
        let regResult = `${str}`.match(reg);
        return regResult ? true : false;
    }
    static createWorkSheets(map: {}): any[] {
        let excelArr = [];
        let langCountrys = [];
        let langExports = [];
        let langTypes = [];
        excelArr.push(langCountrys);
        excelArr.push(langExports);
        excelArr.push(langTypes);
        for (const zh_id in map) {
            const langItem = map[zh_id];
            let lang_arr = [];
            for (const country in langItem) {
                const country_lang = langItem[country];
                let lang_idx = langCountrys.indexOf(country)
                if (lang_idx > -1) {
                    lang_arr[lang_idx] = country_lang;
                } else {
                    langCountrys.push(country);
                    langExports.push('c');
                    if (country == 'nocheck') {
                        langTypes.push('string[]');
                    } else {
                        langTypes.push('string');
                    }
                    lang_arr[langCountrys.length - 1] = country_lang;
                }
            }
            excelArr.push(lang_arr);
        }
        let tag_title = excelArr.shift()
        let tag_export = excelArr.shift()
        let tag_type = excelArr.shift()
        // excelArr.sort((x, y) => {
        //     if (x && x.length && y && y.length) {
        //         return x[0].localeCompare(y[0]);
        //     }
        //     return 0
        // })
        excelArr.unshift(tag_type);
        excelArr.unshift(tag_export);
        excelArr.unshift(tag_title);
        return excelArr;
    }
    /**
     * 生成xlsx文件
     * @param worksheets 内容
     * @param outputPath 文件输出路径
     */
    static writeXlsx(worksheets: { name: string; data: any[][]; options?: {}; }[], outputPath: string) {
        let buffer = xlsx.build(worksheets);
        //@ts-ignore
        fs.writeFileSync(outputPath, buffer, { 'flag': 'w' });
    }
    /**
     * 验证文件是否有效
     * @param filePath 
     * @returns 
     */
    static validFolder(filePath: string) {
        return filePath.indexOf("DS_Store") == -1;
    }
}